package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.config.ErpConfig;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.common.utils.file.FileUploadUtils;
import com.ly.common.utils.uuid.IdUtils;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.mapper.FileInfoMapper;
import com.ly.heart.service.IFileInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileEntity> implements IFileInfoService {

    protected final Logger logger = LoggerFactory.getLogger ( this.getClass () );

    /**
     * 附件上传
     * @param files
     * @param type
     * @return
     */
    @Override
    public List<FileEntity> uploadFiles (MultipartFile[] files, Integer type) {
        if ( files == null || files.length == 0 ) {
            // 可以选择返回空列表或抛出一个异常，这里选择返回空列表
            return new ArrayList<> ();
        }
        List<FileEntity> fileList = new ArrayList<> ();
        for ( MultipartFile file : files ) {
            try {
                FileEntity fileEntity = createFileEntity ( file, type );
                fileList.add ( fileEntity );
            } catch ( Exception e ) {
                // 日志记录文件处理失败的情况
                logger.error ( "Failed to process file: {}", e.getMessage () );
                continue; // 根据需要可以选择跳过失败的文件或停止处理并抛出异常
            }
        }
        boolean saveSuccess = saveBatch ( fileList );
        if ( ! saveSuccess ) {
            // 处理保存失败的情况，这里选择返回空列表
            return new ArrayList<> ();
        }
        return fileList;
    }

    @Override
    public FileEntity uploadFile (MultipartFile file, Integer type) {
        if ( file == null ) {
            return null;
        }
        FileEntity fileEntity = new FileEntity ();
        try {
            fileEntity = createFileEntity ( file, type );
        } catch ( Exception e ) {
            e.printStackTrace ();
        }
        return fileEntity;
    }

    private FileEntity createFileEntity (MultipartFile file, Integer type) throws Exception {
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        // 使用唯一标识符避免使用原始文件名，增加安全性
        String fileName = FileUploadUtils.upload ( ErpConfig.getUploadPath (), file );
        fileEntity.setFileName ( file.getOriginalFilename () );
        fileEntity.setFileUrl ( fileName );
        String fileOnlyName = fileName.substring ( fileName.lastIndexOf ( "/" ) );
        System.out.println ( fileOnlyName );
        System.out.println ( ErpConfig.getUploadPath () );
        fileEntity.setFileOnlyName ( fileOnlyName );
        fileEntity.setType ( type );
        fileEntity.setCreateTime ( DateUtils.getNowDate () );
        return fileEntity;
    }
}
